package com.powernobug.servlet;

import com.powernobug.bean.model.MarketCoupon;
import com.powernobug.bean.model.MarketCouponUser;
import com.powernobug.bean.vo.BaseRespVo;
import com.powernobug.bean.vo.PageVo;
import com.powernobug.bean.vo.data.CouponListuserData;
import com.powernobug.bean.vo.param.CouponQueryParam;
import com.powernobug.service.AdminCouponService;
import com.powernobug.service.impl.AdminCouponServiceImpl;
import com.powernobug.util.BeanUtil;
import com.powernobug.util.JacksonUtil;

import com.powernobug.util.JsonUtil;
import com.powernobug.util.PageUtil;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.beanutils.ConvertUtils;
import org.apache.commons.beanutils.converters.DateConverter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @description:
 * @project: project1-no-bug
 * @package: com.powernobug.servlet
 * @author: HuangWeiLong
 * @date: 2024/8/22 22:36
 */
@WebServlet("/coupon/*")
public class AdminCouponServlet extends CommonServlet{
    static {
        DateConverter dateConverter = new DateConverter(null);
        dateConverter.setPatterns(new String[] {"yyyy-MM-dd", "yyyy-MM-dd HH:mm:ss"});
        ConvertUtils.register(dateConverter, Date.class);
    }
    AdminCouponService adminCouponService=new AdminCouponServiceImpl();
    public BaseRespVo list(HttpServletRequest request){
        CouponQueryParam couponQueryParam = BeanUtil.queryParamPagemant(CouponQueryParam.class, request);
        //2.调用model的进行数据处理
        PageVo data=adminCouponService.list(couponQueryParam);
        //3.返回响应结果
        return BaseRespVo.ok(data);
    }
    public BaseRespVo create(HttpServletRequest request) throws IOException, ServletException {
        String json = request.getReader().readLine();
        Map map = JacksonUtil.read(json, Map.class);
        MarketCoupon marketCoupon = new MarketCoupon();
        try {
            BeanUtils.copyProperties(marketCoupon,map);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        } catch (InvocationTargetException e) {
            throw new RuntimeException(e);
        }
        return adminCouponService.create(marketCoupon);
    }
    public BaseRespVo update(HttpServletRequest request) throws IOException {
        String jrson = request.getReader().readLine();
        Map map = JacksonUtil.read(jrson, Map.class);

        MarketCoupon marketCoupon = new MarketCoupon();
        try {
            BeanUtils.copyProperties(marketCoupon,map);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        } catch (InvocationTargetException e) {
            throw new RuntimeException(e);
        }
        return adminCouponService.update(marketCoupon);
    }

    public BaseRespVo delete(HttpServletRequest request) throws IOException {
        String json = request.getReader().readLine();
        Map map = JacksonUtil.read(json, Map.class);
        MarketCoupon marketCoupon = new MarketCoupon();

        try {
            BeanUtils.copyProperties(marketCoupon,map);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        } catch (InvocationTargetException e) {
            throw new RuntimeException(e);
        }
        if(marketCoupon==null){
            return BaseRespVo.badArgs();
        }
        adminCouponService.delete(marketCoupon);
        return BaseRespVo.ok(null);
    }
    public BaseRespVo read(HttpServletRequest request){
        Integer id = Integer.parseInt(request.getParameter("id"));
        return adminCouponService.read(id);
    }
    public BaseRespVo listuser(HttpServletRequest request){
        Integer page = Integer.parseInt(request.getParameter("page"));
        Integer limit = Integer.parseInt(request.getParameter("limit"));
        Integer couponId = Integer.parseInt(request.getParameter("couponId"));
        String sort = request.getParameter("sort");
        String order = request.getParameter("order");

        List<MarketCouponUser> marketCouponUserList=adminCouponService.listuser(couponId,order,sort);
        long total = marketCouponUserList.size();
        Integer pages = PageUtil.pages(limit, total);

        CouponListuserData couponListuserData = CouponListuserData.builder().total(total)
                .pages(pages)
                .limit(limit)
                .page(page)
                .marketCouponUsers(marketCouponUserList).build();
        return BaseRespVo.ok(couponListuserData);
    }

}
